#pragma once

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector.h>
#include <iostream>

#include <unistd.h>

#include <sys/socket.h>
#include <sys/types.h>

#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>

#include <signal.h>
#include <curses.h>
#include <errno.h>
#include <math.h>

#include "Packet.h"

#define SIZE_BUFFER 10
#define KEY_DELETE 127
#define TEXT_PORT 6716
#define FILE_PORT 6715

class Client {
public:
	Client();
	
	// Run loop
	void run(void);
	
	// Set to true when running
	bool isRunning;
	
protected:
	
	// Struct for messages
	typedef struct {
		uint_32 speaker;
		std::string message;
	} Message;
	
	// Struct for holding a command
	typedef struct {
		char* cmd;
		char* dsc;
		char* use;
	} Command;
	
	// Port for sending text
	int textPort;
	
	// Port for sending file
	int filePort;
	
	// Socket for sending text
	int textSock;
	
	// Socket for sending files
	int fileSock;
	
	// Switcher for 'threading'
	fd_set switcher;
	
	// Host information
	struct hostent *host;
	
	// Connection information
	struct sockaddr_in connection;
	
	// Position of cursor
	int cursor;
	
	// Scroll position
	int scrollPos;
	
	// Number of messages
	int messages;
	
	// Style
	int style;
	
	//
	int characters;
	int convoSize;
	int cmds;
	char message[DATA_SIZE];
	
	// Array of commands (load from file)
	Command commands[10];
	
	Message *conversation;
	char *username;
	
	// Operating Specifics
	virtual void catchInput(void) = 0;
	
	// Add line to chat log
	virtual void addLine(int, const char*, ...) = 0;
	
	// Insert line into conversation
	virtual void addLine(int, int, const char*, ...) = 0;
	
	/*
	 * The toolbar shows information
	 * about the current connection
	 ********************************/
	virtual void updateToolbar(void) = 0;
	virtual void updateWindow(void) = 0;
	virtual void drawConversation(void) = 0;
	
	// Standard Client Functions
	void clearTextField(void);
	
	// Process user commands
	void processCommands(char*);
	
	// Process packet data
	void processIncoming(Packet);
	
	// Initiate file send
	void sendFile(char*);
	
	// Connect to a server
	bool connectTo( const char* ipaddress );
	
	// Disconnect from server
	void disconnect(void);
	
	// Check validity of ipaddress
	bool validIP( const char* ipaddress );
	
	// Load commands from file
	void loadCommands(void);
	
	// Read line from file
	void readLine(char*, FILE*);
	
	// Runtime Functions
	void styles(char*);
	
	// Set Nickname, notify server
	void nick(const char*);
	
	// Display help information
	void help(const char*);
	
	// Display connection information
	void info(const char*);
	
	// Quit client
	void quit( const char*);
};

